package com.zim.ta_service.support.generator.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.system.SystemUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.zim.ta_service.base.enums.OnOffEnum;
import com.zim.ta_service.support.generator.common.vo.GeneratorSettingVo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lhb on 2019/6/21
 */
public class CodeGeneratorBase {

	protected GeneratorSettingVo initSetting() {
		GeneratorSettingVo vo = new GeneratorSettingVo();
		vo.setAuthorName("anonymity");
		vo.setBasePackage("com.delai.abi");
		vo.setDbDriverName("com.mysql.cj.jdbc.Driver");
		vo.setDbUrl("jdbc:mysql://8.134.84.49:11000/pump?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true");
		vo.setDbUserName("root");
		vo.setDbPassword("PumpRoot@2021");

		vo.setServiceGenerateSwitch(OnOffEnum.ON);
		vo.setEntityGenerateSwitch(OnOffEnum.ON);
		vo.setMapperGenerateSwitch(OnOffEnum.ON);
		return vo;
	}

	protected GeneratorSettingVo customSetting(GeneratorSettingVo vo) {
		return vo;
	}

    protected void doGenerator() {
		GeneratorSettingVo vo = initSetting();
		vo = customSetting(vo);

		if (CollUtil.isEmpty(vo.getTableList())) {
			throw new RuntimeException("没有定义需要生成的表清单");
		}

		// 取得当前目录
		String currentDir = SystemUtil.getUserInfo().getCurrentDir();

		CustomAutoGenerator mpg = new CustomAutoGenerator();
		mpg.setBizModuleName(vo.getBizModuleName());

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir(currentDir + "/pump-service/src/main/java");
		gc.setAuthor(vo.getAuthorName());
		gc.setFileOverride(true);
		gc.setEnableCache(false);
		gc.setBaseResultMap(true);
		gc.setBaseColumnList(true);
		gc.setOpen(false);
		gc.setIdType(IdType.AUTO);
		gc.setDateType(DateType.ONLY_DATE);
		gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName(vo.getDbDriverName());
        dsc.setUrl(vo.getDbUrl());
        dsc.setUsername(vo.getDbUserName());
        dsc.setPassword(vo.getDbPassword());
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(vo.getBasePackage());
        pc.setModuleName("");
        mpg.setPackageInfo(pc);

		mpg.setServiceSwitch(false);
		mpg.setEntitySwitch(false);
		mpg.setMapperSwitch(false);

        if (vo.getServiceGenerateSwitch().equals(OnOffEnum.ON)) {
            mpg.setServiceSwitch(true);
        }
        if (vo.getEntityGenerateSwitch().equals(OnOffEnum.ON)) {
			mpg.setEntitySwitch(true);
        }
        if (vo.getMapperGenerateSwitch().equals(OnOffEnum.ON)) {
            mpg.setMapperSwitch(true);
        }

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setSuperControllerClass("");
        strategy.setInclude(vo.getTableList().toArray(new String[0]));
        strategy.setControllerMappingHyphenStyle(true);

		// 共通字段填充
		List<TableFill> fillList = new ArrayList<>();
		fillList.add(new TableFill("created_by", FieldFill.INSERT));
		fillList.add(new TableFill("created_date", FieldFill.INSERT));
		fillList.add(new TableFill("last_updated_by", FieldFill.INSERT_UPDATE));
		fillList.add(new TableFill("last_updated_date", FieldFill.INSERT_UPDATE));
		strategy.setTableFillList(fillList);

        mpg.setStrategy(strategy);
        mpg.execute();
    }
}
